<?php

class MemTypeController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 新建会员等级
	 */
	public function actionCreate()
	{
		$model=new MemType;
		
		if(isset($_POST['MemType']))
		{
			//保存级别
			$model->attributes=$_POST['MemType'];
			$model->zmt_type = 'define';//用户定义会员类型
		//	$model->image=CUploadedFile::getInstance($model,'zm_pic');
			$model->save();
			//保存操作权限
			$privilege = $_POST['privilege'];
			foreach($privilege as $val){
				foreach($val as $key1=>$val1){
					$criteria=new CDbCriteria;
					$criteria->compare('child',$val1);
					$criteria->compare('parent',$model->zmt_id);
					if(!MemPrivilege::model()->exists($criteria)){
						$privilege = new MemPrivilege();
						$privilege->child = $val1;
						$privilege->parent = $model->zmt_id;
						$privilege->save();
					}
					$actions[] = $val1;
				}
			}
			//成功跳转
			$this->redirect(array('admin'));
		}
		//默认会员组是1，普通会员
		$model->zmg_id = 1;
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 *更新会员等级
	 */
	public function actionUpdate($id)
	{
		//实例化模型
		$model=$this->loadModel($id);
		//加载操作权限
		foreach($model->actions as $val){
			$actions[] = $val->child;
		}
		$actions = empty($actions)?array():$actions;

		//保存数据
		if(isset($_POST['MemType']))
		{
			$model->attributes=$_POST['MemType'];
			$model->save();
			//保存操作权限
			$privilege = $_POST['privilege'];
			//删除旧的权限记录
			$criteria=new CDbCriteria;
			$criteria->compare('parent',$model->zmt_id);
			MemPrivilege::model()->deleteAll($criteria);
			//创建新的记录
			foreach($privilege as $val){
				foreach($val as $key1=>$val1){
						$privilege = new MemPrivilege();
						$privilege->zmv_type = 2;
						$privilege->child = $val1;
						$privilege->parent = $model->zmt_id;
						$privilege->save();
					$actions[] = $val1;
				}
			}
			//成功跳转
			$this->redirect(array('admin'));
		}

		$this->render('update',array(
			'model'=>$model,
			'actions'=>$actions,
		));
	}

	/**
	 * 删除会员等级
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		//删除操作权限
		foreach((array)$id as $valid){
			$model=$this->loadModel($valid);
			//删除操作权限
			foreach($model->actions as $val){
				$criteria=new CDbCriteria;
				$criteria->compare('child',$val->child);
				$criteria->compare('parent',$model->zmt_id);
				MemPrivilege::model()->delete($criteria);
			}
		}		
		
		$ids = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zmt_id in ($ids)");
		
		//执行操作
		if(MemType::model()->deleteAll($criteria)){
			//成功跳转
			 $this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('MemType');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new MemType('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['MemType']))
			$model->attributes=$_GET['MemType'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=MemType::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='mem-type-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
